package org.gordonframework.site.forms.elements {
	import org.gordonframework.site.forms.elements.skins.GTextInputSkin;	
	import org.gordonframework.site.forms.elements.GElement;
	
	/**
	 * @author eriarv
	 */
	public class GTextInput extends GElement {
		
		public static const TYPE : String = "TextInput";
		
		private var _disabled : Boolean;
		private var _validation : String;
		private var _maxAnswerTextLength : int;
		private var _helpText : String;
		private var _displayAsPassword : Boolean;

		
		public function GTextInput( 
			id : String, 
			value : String = "", 
			mandatory : Boolean = false, 
			text : String = "", 
			displayAsPassword : Boolean = false,
			disabled : Boolean = false,
			validation : String = "",
			maxAnswerTextLength : int = 100,
			helpText : String = "",
			skinClass : Class = null
		) {
			_displayAsPassword = displayAsPassword;
			_helpText = helpText;
			_maxAnswerTextLength = maxAnswerTextLength;
			_validation = validation;
			_disabled = disabled;
			tabIndexable = true;
			super(TYPE, id, value, mandatory, text, skinClass);
		}

		override protected function parseElementXML(xml : XML) : void {
			super.parseElementXML(xml);
			_disabled = String( xml.@disabled ).toLowerCase( ) == "true";
			_validation = xml.@validation;
			_maxAnswerTextLength = xml.@maxAnswerTextLength;
			_helpText = xml.@helpText;
		}
		
		public override function get value() : String {
			return ( skin as GTextInputSkin ).value;
		}
		
		public override function get isValid() : Boolean {
			var validationIsValid : Boolean = false;
			var inputIsValid : Boolean = false;
			if ( validation.length > 0 && value.length > 0 ) {
				validationIsValid = value.match( new RegExp( validation, "g" ) ).length != 0;
			} else {
				validationIsValid = true;
			}
			if ( mandatory ) {
				inputIsValid = value.length > 0 && validationIsValid;
			} else {
				inputIsValid = validationIsValid;
			}
			
			return inputIsValid;
		}

		public function get disabled() : Boolean {
			return _disabled;
		}
		
		public function set disabled(disabled : Boolean) : void {
			_disabled = disabled;
		}
		
		public function get validation() : String {
			return _validation;
		}
		
		public function set validation(validation : String) : void {
			_validation = validation;
		}
		
		public function get maxAnswerTextLength() : int {
			return _maxAnswerTextLength;
		}
		
		public function set maxAnswerTextLength(maxAnswerTextLength : int) : void {
			_maxAnswerTextLength = maxAnswerTextLength;
		}
		
		public function get helpText() : String {
			return _helpText;
		}
		
		public function set helpText(helpText : String) : void {
			_helpText = helpText;
		}
		
		public function get displayAsPassword() : Boolean {
			return _displayAsPassword;
		}
		
		public function set displayAsPassword(displayAsPassword : Boolean) : void {
			_displayAsPassword = displayAsPassword;
			(skin as GTextInputSkin).displayAsPassword = displayAsPassword;
		}
	}
}
